package lzupdater.gui;

import java.awt.Color;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.util.Collections;
import java.util.Vector;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import lzupdater.com.CommunicationHub;
import lzupdater.com.HubComponent;
import lzupdater.gui.elements.LocalizedJButton;
import lzupdater.gui.elements.LocalizedJLabel;
import lzupdater.gui.listeners.ProfileListener;
import lzupdater.locale.Dictionary;
import lzupdater.locale.LocalizedString;
import lzupdater.utils.LZUProfileManager;
import lzupdater.utils.Profile;
import lzupdater.utils.ProfileManagerListener;

//LZRosterUpdater, a java client to update LZRoster
//Copyright (C) 2008 Fraskor from Krasus (e-mail: fraskor@gmail.com)
//
//This file is part of LZRosterUpdater.
//
//LZRosterUpdater is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this program.  If not, see <http://www.gnu.org/licenses/>.

/**
 * 
 * @author Fraskor
 */
public class ProfileListPanel extends HubComponent implements
		ProfileManagerListener, ProfileListener {

	private final static LocalizedString areYouSureToDeleteProfile = new LocalizedString(
			"Really delete the profile '%s' ?");
	private final static LocalizedString deletionComfirmation = new LocalizedString(
			"Confirm deletion");
	private final static LocalizedString addButtonToolTip = new LocalizedString(
			"Add a new profile (Ctrl+Click to dupplicate the current pofile)");

	private JPanel panel;
	private JList jList;
	private LocalizedJLabel profileListLabel;
	private LocalizedJButton addButton;
	private LocalizedJButton deleteButton;

	private LZUProfileManager profileManager;

	private Vector<String> profileNames;

	public ProfileListPanel(CommunicationHub hub) {
		super(hub);
		panel = new JPanel();
		jList = new JList();
		profileListLabel = new LocalizedJLabel("Profiles");

		addButton = new LocalizedJButton("+");
		deleteButton = new LocalizedJButton("-");
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void initializeCommunication() {
		profileManager = hub.getComponent(LZUProfileManager.class);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void initialize() {
		initializeGui();
	}

	/**
	 * {@inheritDoc}
	 */
	public void setProfileList(Object[] profileNames) {
		this.profileNames = new Vector<String>(profileNames.length);

		for (int i = 0; i < profileNames.length; i++)
			this.profileNames.add(profileNames[i].toString());

		Collections.sort(this.profileNames);

		int idx = findIdxOfCurrentProfile();
		jList.setListData(this.profileNames);
		jList.setSelectedIndex(idx);
	}

	private int findIdxOfCurrentProfile() {
		if (profileManager.getCurrentProfile() != null && profileNames != null) {
			String name = profileManager.getCurrentProfile().getName();
			for (int i = 0; i < profileNames.size(); i++) {
				if (name.equals(profileNames.get(i)))
					return i;
			}
		}
		return -1;
	}

	private void initializeGui() {
		GridBagLayout layout = new GridBagLayout();
		panel.setLayout(layout);

		GridBagConstraints c = new GridBagConstraints();
		c.insets = new Insets(5, 5, 5, 5);

		c.fill = GridBagConstraints.BOTH;
		c.anchor = GridBagConstraints.WEST;
		c.weightx = 1;
		c.weighty = 0;
		c.gridx = 0;
		c.gridy = 0;
		panel.add(profileListLabel, c);

		c.fill = GridBagConstraints.BOTH;
		c.anchor = GridBagConstraints.WEST;
		c.weightx = 0;
		c.weighty = 0;
		c.gridx = 1;
		c.gridy = 0;
		panel.add(addButton, c);

		addButton.setLocalizedToolTipText(addButtonToolTip);

		c.fill = GridBagConstraints.BOTH;
		c.anchor = GridBagConstraints.WEST;
		c.weightx = 0;
		c.weighty = 0;
		c.gridx = 2;
		c.gridy = 0;
		panel.add(deleteButton, c);

		c.fill = GridBagConstraints.BOTH;
		c.anchor = GridBagConstraints.NORTH;
		c.weightx = 1;
		c.weighty = 1;
		c.gridx = 0;
		c.gridy = 1;
		c.gridwidth = 3;
		c.insets = new Insets(5, 5, 5, 5);
		jList.setBorder(BorderFactory.createLineBorder(Color.black));
		panel.add(jList, c);

		jList.addListSelectionListener(new ListSelectionListener() {

			public void valueChanged(ListSelectionEvent e) {
				Object selecteditem = jList.getSelectedValue();
				if (selecteditem != null)
					profileManager.setCurrentProfile(selecteditem.toString());
				else {
					profileManager.setCurrentProfile(null);
				}
			}

		});

		initialiseAction();

		profileManager.addProfileManagerListener(this);
		profileManager.addProfileListener(this);
	}

	private void initialiseAction() {
		addButton.addActionListener(new AbstractAction() {
			/**
			 * 
			 */
			private static final long serialVersionUID = 9173544881338283593L;

			public void actionPerformed(ActionEvent e) {
				Object item = jList.getSelectedValue();
				if (item != null
						&& (e.getModifiers() & ActionEvent.CTRL_MASK) != 0) {
					profileManager.cloneProfile(item.toString());
				} else {
					profileManager.addNewProfile();
				}
			}
		});

		deleteButton.addActionListener(new AbstractAction() {

			/**
			 * 
			 */
			private static final long serialVersionUID = 8492946955401279798L;

			public void actionPerformed(ActionEvent arg0) {
				Object item = jList.getSelectedValue();
				if (item == null)
					return;

				Object[] options = { Dictionary.translate("Yes"),
						Dictionary.translate("No") };
				int n = JOptionPane.showOptionDialog(getGui(),
						areYouSureToDeleteProfile.useAsFormater(item),
						deletionComfirmation.toString(),
						JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE,
						null, // do not use a custom Icon
						options, // the titles of buttons
						options[0]); // default button title

				if (n == 0) {
					profileManager.deleteProfile(item.toString());
				}
			}

		});

	}

	public Component getGui() {
		return panel;
	}

	public void setProfile(Profile profile) {
		jList.setSelectedIndex(findIdxOfCurrentProfile());
	}

}
